<?php
defined('BY_JYA') or exit('error');
if($_SESSION['os']!='windows'){
	cfc('core')->checklogin();
}
if(!$_SESSION['uid']){
	exi('','','/manage.login/');
}
$_RQ['op']=$_RQ['op']?$_RQ['op']:'get';
$setting = plugin('jy_merch')->plugin_setting('setting');
set_html('title','注册商户');
if($_RQ['op']=='get'){
	if(!$_RQ['add']){
		$item=pdo_get('jy_merch_user',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']));
		if($item){
			if($item['status']==1){
				exi('','error','jy_merch/manage.index/');
			}
		}
	}
	if($setting['is_level']){
		$levelid=pdo_getcolumn('jy_weishop_member',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']),'level');
		if(!$levelid){
			exi('您当前等级不能申请开通！','error');
		}
	}
	if(empty($item)){
		$member = pdo_get('core_members',array('id'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']),array('id','fromuser','realname','credit2','mobile','groupid','avatar','nickname'));
		$item =array(
			'merchname'=>$member['nickname'],
			'realname'=>$member['realname'],
			'mobile'=>$member['mobile'],
			'logo'=>$member['avatar'],
		);
	}
	$return['level']=pdo_get('jy_area_psi_level',array('uniacid'=>$_SESSION['uniacid'],'ismerch'=>1));
	$conditions=json_decode($return['level']['conditions'],true);
	$level_up_types=$this->model()->current_level_up_types();
	$up=true;
	$upmsg='';
	foreach($level_up_types as $type){
		if($type=='buy_goods' && count($conditions['goods_ids'])){
			$goods_ids=$conditions['goods_ids'];
			foreach($goods_ids as $gv){
				$up=false;
				$isbuynum=pdo_getsum('jy_weishop_order_goods',array('uniacid'=>$_SESSION['uniacid'],'ispay'=>1,'mid'=>$_SESSION['uid'],'goodsid'=>$gv['id']),'total');
				$goodstitle=pdo_getcolumn('jy_weishop_goods',array('uniacid'=>$_SESSION['uniacid'],'id'=>$gv['id']),'title');
				if($isbuynum>0){
					$up=true;
					break;
				}else{
					$upmsg.='需购买商品：【'.$goodstitle.'】；';
				}
			}
		}
		if($type=='level_num' && $conditions['level_num']){
			if(strpos($conditions['level_id'], 'a') !== false){
				$member['level_num']=pdo_count('jy_area_psi_agent_area',array('uniacid'=>$_SESSION['uniacid'],'level_id'=>$conditions['level_id'],'fromuser'=>$_SESSION['uid']));
			}else{
				$shoplevelid=pdo_getcolumn('jy_area_psi_level',array('uniacid'=>$_SESSION['uniacid'],'id'=>$conditions['level_id']),'shoplevelid');
				$title=pdo_getcolumn('jy_area_psi_level',array('uniacid'=>$_SESSION['uniacid'],'id'=>$conditions['level_id']),'title');
				$member['level_num']=pdo_count('jy_weishop_member',array('uniacid'=>$_SESSION['uniacid'],'level'=>$shoplevelid?:0,'fromuser'=>$_SESSION['uid']));
			}
			if($member['level_num']<$conditions['level_num']){
				$up=false;
				$upmsg.='需发展：【'.$title.'】'.$conditions['level_num'].'位；';
				break;
			}
		}
		if($type=='myorder_price' && $conditions['myorder_price']){
			$member['myorder_price']=pdo_getsum('jy_weishop_order',array('uniacid'=>$_SESSION['uniacid'],'ispay'=>1,'mid'=>$_SESSION['uid']),'price');
			if($member['myorder_price']<$conditions['myorder_price']){
				$up=false;
				$upmsg.='需自购订单总金额满：【'.$conditions['myorder_price'].'】；';
				break;
			}
		}
		if($type=='order_num' && $conditions['order_num']){
			$member['order_num']=pdo_count('jy_weishop_order',array('uniacid'=>$_SESSION['uniacid'],'ispay'=>1,'mid'=>$_SESSION['uid']));
			if($member['order_num']<$conditions['order_num']){
				$up=false;
				$upmsg.='需自购订单数量满：【'.$conditions['order_num'].'】；';
				break;
			}
		}
		if($type=='yang_jinum' && $conditions['yang_jinum']){
			$member['yang_jinum']=pdo_count('jy_yangjidan_myji',array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
			if($member['yang_jinum']<$conditions['yang_jinum']){
				$up=false;
				$upmsg.='认养小鸡：【'.$conditions['yang_jinum'].'】只；';
				break;
			}
		}
	}
	$return['up']=$up;
	$return['upmsg']=$upmsg;
	$return['set']=$setting;
	$item['lat_lng']=$item['lat_lng']?json_decode($item['lat_lng'],true):'';
	$item['codeimg']=$item['codeimg']?json_decode($item['codeimg'],true):array();
	$item['pcd']=$item['pcd']?json_decode($item['pcd'],true):array('city'=>'');
	$item['addressinfo']=array('address'=>$item['address'],'lat_lng'=>$item['lat_lng']);
	$item['zhaoshangid']=intval($_RQ['zhaoshangid']);
	$item['upass']='';
	$item['diyfields']=$setting['diyformfields']?:array();
	$return['cate']=pdo_getall('jy_merch_category',array('uniacid'=>$_SESSION['uniacid']),array('id','catename'));
	foreach($return['cate'] as $k=>&$gv){
		if($item['cateid']==$gv['id']){
			$item['pcateindex']=$k;
		}
	}
	$return['post']=$return['item']=$item;
	$return['set']['regbg']=tomedia($return['set']['regbg']);
	if($_SESSION['wxapp_id']){
		$return['tmplIds']=array();
	}
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	if($_SESSION['container']!='wxapp' && $return['basic']['is_smscode'] && !check_sms_code($postdata['smscode'])){
		exi('手机验证码错误！','error');
	}
	if(!$postdata['realname']){
		exi('请填写姓名','error');
	}
	if(!$postdata['mobile']){
		exi('请填写手机号','error');
	}
	if(strlen($postdata['mobile'])!=11){
		exi('请填写正确的手机号','error');
	}
	if(!$postdata['pcd']['province'] || !$postdata['pcd']['city']){
		exi('请选择城市区域','error');
	}
	if(!$postdata['addressinfo']['lat_lng']){
		exi('请填写正确的坐标','error');
	}
	if(!$postdata['cateid']){
		//exi('请选择商户分类！','error');
	}
	if(!$postdata['uname']){
		exi('请填写用户名','error');
	}
	if(!$postdata['check'] && $setting['applytitle'] && $setting['applycontent']){
		exi('请先阅读协议！','error');
	}
	$level=pdo_get('jy_area_psi_level',array('uniacid'=>$_SESSION['uniacid'],'ismerch'=>1));
	$postdata['address']=$postdata['addressinfo']['address'];
	$postdata['lat_lng']=$postdata['addressinfo']['lat_lng'];
	$data =array(
		'uname'=>$postdata['uname'],
		'applytime'=>time(),
		'mobile'=>$postdata['mobile'],
		'realname'=>$postdata['realname'],
		'merchname'=>$postdata['merchname'],
		'desc'=>$postdata['desc'],
		'salecate'=>$postdata['salecate'],
		'cateid'=>$postdata['cateid'],
		'status'=>$level['price']>0?0:1,		
		'jointime'=>time(),
		'address'=>$postdata['address'],
		'pcd'=>ijson_encode($postdata['pcd']),
		'lat_lng'=>ijson_encode($postdata['lat_lng']),
		'codeimg'=>ijson_encode($postdata['codeimg']),		
		'zhaoshangid'=>$postdata['zhaoshangid'],
		'diyformdata'=>ijson_encode($postdata['diyfields']),
	);
	$ischeckauth=model('jy_merch/model')->checkauth($postdata['id'],true);
	if($ischeckauth==3){
		$data['status']=3;
	}
	if($postdata['upass']){
		$data['upass']=base64_encode($postdata['upass']);
	}
	$isadd=pdo_get('jy_merch_user',array('mobile'=>$data['mobile'],'uniacid'=>$_SESSION['uniacid']),array('id'));
	if($postdata['id']){
		if($isadd['id']!=$postdata['id'] && $setting['ismobile']){
			exi('该手机号已申请过！','error');
		}
		pdo_update('jy_merch_user',$data,array('id'=>$postdata['id'],'mid'=>$_SESSION['uid']));
	}else{
		$log=pdo_get('jy_merch_user',array('uniacid'=>$_SESSION['uniacid'],'uname'=>$postdata['uname']));
		if($log){
			exi('用户名已被注册','error');
		}
		if($isadd['id'] && $setting['ismobile']){
			exi('该手机号已申请过！','error');
		}
		$data['uniacid']=$_SESSION['uniacid'];
		$data['mid']=$_SESSION['uid'];
		$data['viptime']=TIMESTAMP+365*24*3600;
		pdo_insert('jy_merch_user',$data);
	}
	if($_SESSION['current_plugin'] && $_SESSION['current_plugin']!='core'){
		$url=$_SESSION['forward_url']?:$_SESSION['current_plugin'].'/index/';
	}else{
		$url='jy_merch/index/';
	}
	if($level['price']>0){
		$insert=array(
			'uniacid'=>$_SESSION['uniacid'],
			'mid'=>$_SESSION['uid'],
			'level_id'=>$level['id'],
			'fee'=>$level['price'],
			'createtime'=>TIMESTAMP
		);
		pdo_insert('jy_area_psi_yajin_log',$insert);
		$order_id=pdo_insertid();
		$params=array(
			'id'=>$order_id,
			'title'=>'私厨申请费用',
			'fee'=>$level['price'],
			//'pay_type'=>'wechat',//可选，此项作用为指定支付方式
			'no_type'=>array('hdfk'),//可选，此项作用为指定不允许使用哪种支付方式
			'back_url'=>'jy_area_psi/register/payresult/',//必填，指定异步支付结果通知地址，采用系统标准URL书写格式
			'success_url'=>$success_url?:'jy_area_psi/register/',
			'error_url'=>$error_url?:'jy_area_psi/register/',
		);
		$payid=cfc('pay')->create_pay_order($params);
		$this->pdo_update('yajin_log',array('pay_id'=>$payid),array('uniacid'=>$_SESSION['uniacid'],'id'=>$order_id));
		$res=array(
			'pay_id'=>$payid,
			'order_id'=>$order_id,
			'success_url'=>$success_url,
			'error_url'=>$error_url,
		);
		exi($res);
	}
	if($postdata['issendsubmsg']){
		$url='jy_merch/manage.index/';
		$res=array(
			'url'=>$url,
		);
		exi($res);
	}else{
		if($ischeckauth==3){
			exi('已申请成功！','','jy_merch/manage.setyajin/');
		}elseif($setting['isshenhe']){
			exi('已申请,请等待审核','',$url);
		}else{
			$url='jy_merch/manage.index/';
			exi('入驻成功！','',$url);
		}
	}
}
if($_RQ['op']=='payresult'){
	$payresult=$_SESSION['pay_result'];
	if($payresult['status']!=1 && $payresult['pay_type'] !='hdfk'){
		exi('支付失败。','error','jy_area_psi/index/');
	}
	$order=pdo_get('jy_area_psi_yajin_log',array('id'=>$payresult['id']));
	if($order['ispay'] ==1){
		exi('该订单已经支付！','error','jy_area_psi/index/');
	}
	if($order['fee']!=$payresult['fee']){
		exi('支付失败！订单金额不匹配！'.$order['fee'].'|'.$payresult['fee'],'error','jy_area_psi/index/');
	}
	pdo_update('jy_merch_user',array('status'=>1),array('uniacid'=>$_SESSION['uniacid'],'mid'=>$order['mid']));
	$level=pdo_get('jy_area_psi_level',array('uniacid'=>$_SESSION['uniacid'],'ismerch'=>1));
	pdo_update('jy_weishop_member',array('level'=>$level['shoplevelid']),array('merchid'=>0,'uniacid'=>$_SESSION['uniacid'],'mid'=>$order['mid']));
	$this->update('member',array('is_agent'=>1),array('mid'=>$order['mid'],'is_agent'=>0,'merchid'=>$order['merchid']));
	$update=array(
		'paytype'=>$payresult['pay_type'],
		'transid'=>$payresult['transaction_id'],
	);
	pdo_update('jy_area_psi_yajin_log',$update,array('id'=>$payresult['id']));
	exi('提交成功!');
}

//获取小程序订阅消息ID
if($_RQ['op']=='get_tmpids'){
	$tmplIds=array();
	$tmpid=cfc('wxapp_api')->get_tpl_template_id('10415',[5,4,2],'shenhe','审核结果通知','jy_merch');
	if($tmpid){
		$tmplIds[]=$tmpid;
	}
	$res['list']=$tmplIds;
	exi($res);
}
?>